package cn.com.jslh.boarpro.util;

import android.app.Dialog;
import android.content.Context;
import android.graphics.PointF;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.dlazaro66.qrcodereaderview.QRCodeReaderView;

import cn.com.jslh.boarpro.R;


/**
 * 作者：周洲 on 2017/8/1 16:52
 * <p>
 * 邮箱：double0zhou@126.com
 */

public class ScanDialog extends Dialog {

//    private ScannerView scannerView;

    public ScanDialog(@NonNull Context context) {
        super(context);
    }

    public ScanDialog(Context context, int themeid) {
        super(context, themeid);
    }

    public static class Builder {
        private Context context;

        private QRCodeReaderView.OnQRCodeReadListener qrCodeReadListener;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setQrCodeReadListener(QRCodeReaderView.OnQRCodeReadListener qrCodeReadListener){
            this.qrCodeReadListener = qrCodeReadListener;
            return this;
        }

        public ScanDialog create() {

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final ScanDialog dialog = new ScanDialog(context, R.style.Theme_AppCompat_Dialog);
            View layout = inflater.inflate(R.layout.activity_scan, null);
            dialog.addContentView(layout, new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));

            QRCodeReaderView qrCodeReaderView = (QRCodeReaderView) layout.findViewById(R.id.qrdecoderview);

            qrCodeReaderView.setOnQRCodeReadListener(new QRCodeReaderView.OnQRCodeReadListener() {
                @Override
                public void onQRCodeRead(String text, PointF[] points) {
                    if (qrCodeReadListener != null)
                        qrCodeReadListener.onQRCodeRead(text, points);
                    dialog.dismiss();
                }
            });

            // Use this function to enable/disable decoding
            qrCodeReaderView.setQRDecodingEnabled(true);

            // Use this function to change the autofocus interval (default is 5 secs)
            qrCodeReaderView.setAutofocusInterval(2000L);

            // Use this function to enable/disable Torch
            qrCodeReaderView.setTorchEnabled(true);

            // Use this function to set front camera preview
            qrCodeReaderView.setFrontCamera();

            // Use this function to set back camera preview
            qrCodeReaderView.setBackCamera();
            return dialog;
        }
    }

}
